/*
 编程实现控制台版并支持两人对战的五子棋游戏。
（1）绘制棋盘 - 写一个成员方法实现
（2）提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现。
（3）每当一方下棋后判断是否获胜 - 写一个成员方法实现。
 */


import java.util.Scanner;

public class Test2<judge> {
    int[][] ary=new int[16][16];
    boolean judge=false;
    Scanner scanner=new Scanner(System.in);
    int countBlack=0;
    int  countWhite=0;
//显示棋盘
public void chessBoard(){
    char c1='a';
    char c2='a';

    for (int i=0;i<17;i++){
        //第一行
        if (i==0){
            for (int j=0;j<=16;j++){
                //用空格表示第一个
                if (j==0){
                    System.out.print("   ");
                }
                //第一行第二个开始是0到9的数字
                if (j>0&&j<=10){
                    System.out.print("   "+(j-1));
                }
                //第一行从第10个开始是a到f的字母
                if (j>10){
                    System.out.print("   "+ c1++);
                }
            }
            System.out.println();
        }
        //第2到第10行
        if(i>0&&i<=10){
            for (int k=0;k<17;k++){
                //用1到9的数字表示第一个
                if (k==0){
                    System.out.print("  "+(i-1));
                }
                //之后是用加号表示
                if (k>0){
                    if (ary[i-1][k-1]==0){
                        System.out.print("   +");
                    }
                    else if (ary[i-1][k-1]==1){
                        System.out.print("   1");
                    }
                    else if (ary[i-1][k-1]==2){
                        System.out.print("   2");
                    }

                }
            }
            System.out.println();
        }
        //第9行之后
        if (i>10){
            for (int l=0;l<17;l++){
                //第一个是用a到f的字母表示第一个
                if (l==0){
                    System.out.print("  "+c2++);
                }
                //之后是用加号表示
                if (l>0){
                    if (ary[i-1][l-1]==0){
                        System.out.print("   +");
                    }
                    else if (ary[i-1][l-1]==1){
                        System.out.print("   1");
                    }
                    else if (ary[i-1][l-1]==2){
                        System.out.print("   2");
                    }
                }
            }
            //换行
            System.out.println();
        }
    }
}
//黑方和白方分别下棋
public void playChess(boolean judge) {

   if (!judge){
       System.out.println("请黑方下棋");

   }else {
       System.out.println("请白方下棋");
   }
        this.chessBoard();
        System.out.println("请输入你所要下棋的编号位置为");
        System.out.println("请输入行的位置");
        String r = scanner.next();
        System.out.println("请输入列的位置");
        String c = scanner.next();
        boolean b2 = false;
        boolean b3 = false;
        //创建字符串数组存放棋盘的范围
        String[] s={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
        //判断是否超过棋盘的范围0~9
        for (int i=0;i<s.length;i++){
            if (r.equals(s[i])){
                b2=true;
                             break;
            }else {
                b2=false;
            }
        }
    //判断是否超过棋盘的范围a~f
    for (int i=0;i<s.length;i++){
        if (c.equals(s[i])){
            b3=true;
            break;
        }else {
            b3=false;
        }
    }
    //超过范围重新选择
        if (b2==false||b3==false){
            System.out.println("该位置超出棋盘范围，请重新选择你所下棋的位子");
            return;
        }

       int  row = r.charAt(0);
        int column = c.charAt(0);
        if ((row >= 48 && row < 59) && (column >= 48 && column < 59)) {
            if (ary[row - 48][column - 48]==1||ary[row - 48][column - 48]==2){
                System.out.println("该位置已有棋子,请重新下棋");
                return;
            }
            if (!judge){
                ary[row - 48][column - 48] = 1;
            }
            else {
                ary[row - 48][column - 48] = 2;
            }
        }
        else if ((row >= 48 && row < 59) && column>=97&&column<=102) {
            if ( ary[row-48][column - 87]==1|| ary[row-48][column - 87]==2){
                System.out.println("该位置已有棋子,请重新下棋");
                return;
            }
            if (!judge){
                ary[row-48][column - 87] = 1;
            }
            else {
                ary[row-48][column - 87] = 2;
            }

        }else if (row>=97&&row<=102&&((column >= 48 && column < 59))){
            if ( ary[row-87][column - 48] ==1||ary[row-87][column - 48] ==2){
                System.out.println("该位置已有棋子,请重新下棋");
                return;
            }
            if (!judge){
                ary[row-87][column - 48] = 1;
            }
            else {
                ary[row-87][column - 48] = 2;
            }

        }else if (row>=97&&row<=102&&column>=97&&column<=102){
            if ( ary[row-87][column - 87] ==1||ary[row-87][column - 87] ==2){
                System.out.println("该位置已有棋子,请重新下棋");
                return;
            }
            if (!judge){
                ary[row-87][column - 87] = 1;
            }
            else {
                ary[row-87][column - 87] = 2;
            }
        }

        this.judge=!judge;
        this.chessBoard();
    }
//获胜的条件
public boolean gameVictory(){
    for (int y=0;y<ary.length;y++){
        for (int x=0;x<ary[y].length;x++){
            //向左判断
            if (x>3){
                for (int i=0;i<=4;i++){
                   if (ary[y][x-i]==1){
                        countBlack++;
                       if (countBlack==5){
                           System.out.println("恭喜黑方获胜");
                           return  false;
                       }
                   }
                    if (ary[y][x-i]==2){
                        countWhite++;
                        if (countWhite==5){
                            System.out.println("恭喜白方获胜");
                            return  false;
                        }
                    }
                }
                countWhite=0;
                countBlack=0;
            }
            //向右判断
            if (x<=11){
                for (int i=0;i<=4;i++){
                    if (ary[y][x+i]==1){
                        countBlack++;
                        if (countBlack==5){
                            System.out.println("恭喜黑方获胜");
                            return  false;
                        }
                    }
                    if (ary[y][x+i]==2){
                        countWhite++;
                        if (countWhite==5){
                            System.out.println("恭喜白方获胜");
                            return  false;
                        }
                    }
                }
                countWhite=0;
                countBlack=0;
            }
            //向上判断
            if (y>3){
                for (int i=0;i<=4;i++){
                    if (ary[y-i][x]==1){
                        countBlack++;
                        if (countBlack==5){
                            System.out.println("恭喜黑方获胜");
                            return  false;
                        }
                    }
                    if (ary[y-i][x]==2){
                        countWhite++;
                        if (countWhite==5){
                            System.out.println("恭喜白方获胜");
                            return  false;
                        }
                    }
                }
                countWhite=0;
                countBlack=0;
            }
            //向下判断
            if (y<=11){
            for (int i=0;i<=4;i++){
                    if (ary[y+i][x]==1){
                        countBlack++;
                        if (countBlack==5){
                            System.out.println("恭喜黑方获胜");
                            return  false;
                        }
                    }
                    if (ary[y+i][x]==2){
                        countWhite++;
                        if (countWhite==5){
                            System.out.println("恭喜白方获胜");
                            return  false;
                        }
                    }
                }
                countWhite=0;
                countBlack=0;}
            //向左斜上
            if (x>3&&y>3)
            {
                for (int i=0;i<=4;i++){
                if (ary[y-i][x-i]==1){
                    countBlack++;
                    if (countBlack==5){
                        System.out.println("恭喜黑方获胜");
                        return  false;
                    }
                }
                if (ary[y-i][x-i]==2){
                    countWhite++;
                    if (countWhite==5){
                        System.out.println("恭喜白方获胜");
                        return  false;
                    }
                }
            }
                countWhite=0;
                countBlack=0;
            }
            //向左斜下
            if (x>3&&y<=11){
                for (int i=0;i<=4;i++){
                if (ary[y+i][x-i]==1){
                    countBlack++;
                    if (countBlack==5){
                        System.out.println("恭喜黑方获胜");
                        return  false;
                    }
                }
                if (ary[y+i][x-i]==2){
                    countWhite++;
                    if (countWhite==5){
                        System.out.println("恭喜白方获胜");
                        return  false;
                    }
                }
            }
                countWhite=0;
                countBlack=0;}
            //向右斜上
            if (x<=11&&y>3){
                for (int i=0;i<=4;i++){
                    if (ary[y-i][x+i]==1){
                        countBlack++;
                        if (countBlack==5){
                            System.out.println("恭喜黑方获胜");
                            return  false;
                        }
                    }
                    if (ary[y-i][x+i]==2){
                        countWhite++;
                        if (countWhite==5){
                            System.out.println("恭喜白方获胜");
                            return  false;
                        }
                    }
                }
                countWhite=0;
                countBlack=0;
            }
//            向右斜下
            if (x<=11&&y<=11){for (int i=0;i<=4;i++){
            if (ary[y+i][x+i]==1){
                countBlack++;
                if (countBlack==5){
                    System.out.println("恭喜黑方获胜");
                    return  false;
                }
            }
            if (ary[y+i][x+i]==2){
                countWhite++;
                if (countWhite==5){
                    System.out.println("恭喜白方获胜");
                    return  false;
                }
            }
        }
            countWhite=0;
            countBlack=0;
            }
    }
    }


    return true;
}
//开始游戏
public void playGame(){
    System.out.println("选择黑方请按1，选择白方请按2");
    int choice=scanner.nextInt();
    switch(choice){
        case 1:this.judge=false;
             break;
        case 2:this.judge=true;
            break;
    }

    do {
               this.playChess(judge);
    }while(this.gameVictory());
}






public static void main(String[] rags){
    Test2 t=new Test2();
     t.playGame();


}

}
